﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpfTool
{
    /// <summary>
    /// Interaction logic for SliderBox.xaml
    /// </summary>
    public partial class SliderBox : UserControl, IPropertyGridItem
    {
        int id = 0;
        bool intMode = false;
        bool valueChanged = false;

        public SliderBox()
        {
            InitializeComponent();

            theSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(OnValueChangedHandler);

            theSlider.Maximum = 100.0;
            theSlider.Minimum = 0.0;
        }

        public SliderBox(double highValue, double lowValue, bool intMode)
        {
            InitializeComponent();

            this.intMode = intMode;

            theSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(OnValueChangedHandler);

            if (highValue < lowValue)
            {
                double temp = lowValue;
                lowValue = highValue;
                highValue = temp;
            }
            theSlider.Maximum = highValue;
            theSlider.Minimum = lowValue;
        }

        public double SliderValue
        {
            get { return theSlider.Value; }
            set { theSlider.Value = value; }
        }

        private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
        {
            theLabel.Visibility = Visibility.Collapsed;
            theSlider.Visibility = Visibility.Visible;
        }

        private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
        {
            theLabel.Visibility = Visibility.Visible;
            theSlider.Visibility = Visibility.Collapsed;

            if (valueChanged)
            {
                SliderValueChangedEvent(this, SliderValue);
                valueChanged = false;
            }
        }

        void OnValueChangedHandler(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
        {
            if (!intMode)
                theLabel.Text = e.NewValue.ToString();
            else
            {
                int value = System.Convert.ToInt32(e.NewValue);
                theLabel.Text = value.ToString();
            }
            SliderValue = e.NewValue;
            valueChanged = true;
        }


        public event Window1.PropertyGridItemChangeHandler SliderValueChangedEvent;

        void IPropertyGridItem.registerItemUpdateEvent(wpfTool.PropertyGrid grid)
        {
            SliderValueChangedEvent += new Window1.PropertyGridItemChangeHandler(grid.OnGridItemChanged);
        }

        void IPropertyGridItem.unregisterItemUpdateEvent(wpfTool.PropertyGrid grid)
        {
            SliderValueChangedEvent -= grid.OnGridItemChanged;
        }

        int IPropertyGridItem.getID()
        {
            return id;
        }


        void IPropertyGridItem.setID(int i)
        {
            if (id != 0)
            {
                MessageBox.Show("ID Exception!");
                return;
            }

            id = i;
        }

    }
}
